import scrapy

from scrapy_xuezhouyi.items import ZpItem


class XuezhouyiSpider(scrapy.Spider):
    name = 'xuezhouyi'
    allowed_domains = ['www.xuezhouyi.com']
    start_urls = ['https://www.xuezhouyi.com/jiemeng/list_15_1.html']
    page = 1

    def parse(self, response):
        max_page = response.css("q-card-section.justify-center > ul > li:nth-last-child(2)").re_first(r'\d+')
        max_page = int(max_page)
        for item in response.css("q-list.atc-list > a"):
            href = item.attrib['href']
            detail_url = "https://www.xuezhouyi.com{}".format(href)

            item_one = ZpItem()
            item_one['title'] = item.css("h3.title::text").extract_first()
            item_one['description'] = item.css("div.intro::text").extract_first()
            item_one['dataUrl'] = href
            
            yield scrapy.Request(url=detail_url,callback=self.parse_detail,meta={'item':item_one})

        self.page += 1
        next_page_url = "https://www.xuezhouyi.com/jiemeng/list_15_{}.html".format(self.page)
        
        #递归调用，循环打开下一页
        if(self.page <= max_page):
            yield scrapy.Request(url=next_page_url,callback=self.parse)
    
    def parse_detail(self, response):
        item = response.meta['item']
        item["detail"] = response.css("q-card-section.body > div.atc-box").extract_first()
        
        yield item